
import { executeSql } from "../util/db.js";

//-----------前台
//--1.文章搜索
export const selectArticlesCondition = function(startIndex,pageSize,type,key){
    let sql = "select * from articles where isDelete=0 "
    let params = [];
    if(type!=null&&type!=""){
        sql += " and categoryId = ?"
        params.push(type)
    }
    if(key!=null&&key!=""){
        sql += " and title like  ?"
        params.push("%"+key+"%")
    }
    //动态实例
    // for (let key in condition) {
    //     if(key=="equies"){
    //         for (let field in condition[key]) {
    //             sql += ` and ${field}=? `
    //             params.push(condition[key][field])
    //         }
    //     }
    // }
    sql += " limit ?,?"
    // console.log(sql)
    params.push(startIndex);
    params.push(pageSize);
    return executeSql(sql,params)
}

//--3.热点图片
export const selectArticlesCount = function(){
    let sql = "select count(id) count from articles";
    return executeSql(sql);
}
export const selectArticlesRandom = function(start=0){
    let sql = "select id,cover,title from articles limit ?,5";
    return executeSql(sql,[start]);
}

//--4.文章热门排行
export const selectArticlesRead = function(){
    let sql = "select id,title from articles order by `read` desc limit 7"
    return executeSql(sql);
}

//--5.最新资讯
export const selectArticlesNew = function(){
    let sql = `select a.id,a.title,a.content,a.cover,c.name,a.read,count(a.id) comments,a.date 
                from articles a left join comments b on a.id = b.articleId
                                left join categories c on a.categoryId = c.id
                                group by a.id order by a.date desc limit 5 `
    return executeSql(sql);
}

//--7.焦点关注
export const selectArticlesContent = function(start=0){
    let sql = "select content from articles limit ?,7";
    return executeSql(sql,[start]);
}

//--8.文章详情内容
export const selectArticlesarticle = function(id){
    let sql = `select a.title,a.author,t.name type,a.date,a.read,count(a.id),a.content
                from articles a left join categories t on a.categoryId = t.id
                                left join comments c on a.id = c.articleId
                where isDelete=0 and a.id=?`
    return executeSql(sql,[id])
}

export const selectArticlesarticlePre = function(cid){
    let sql = "select id,title from articles where isDelete=0 and id<? order by id desc limit 1"
    return executeSql(sql,[cid])
}

export const selectArticlesarticleNext = function(cid){
    let sql = "select id,title from articles where isDelete=0 and id>? order by id asc limit 1"
    return executeSql(sql,[cid])
}


//-------------后台
//san条用户
export const selectUser = function(){
    let sql = "select nickname,userPic from users"
    return executeSql(sql);
}

export const selectAllUser = function(){
    let sql = "select username,nickname,email,userPic from users"
    return executeSql(sql);
}

//--文章分类删除
export const selectArticlesById = function(cid){
    let sql = "select * from articles where categoryId=?"
    return executeSql(sql,[cid])
}

//--发布文章
export const insertArticle = function(title,covers,categoryId,content){
    let sql = "insert into articles values (?,?,?,curdate(),?,'草稿')"
    return executeSql(sql,[title,covers,categoryId,content])
}
//
export const selectArticleQuery = function(){
    let sql = ""
}
//
export const selectArticleSearch = function(id){
    let sql = "select id,title,cover,categoryId,date,content from articles where id=?"
    return executeSql(sql,[id])
}
//
export const updateArticle = function(){
    let sql = "update articles set title=?,cover=?,categoryId=?,data=?,content=?,state='已发布'"
}

//
export const selectArticleEdit = async function(id,title,cover,categoryId,date,content){
  let sql = "update articles set  title=?,cover=?, categoryId=?,date=?,content=?,state='已发布' where id=?;";
  return await executeSql(sql,[title,cover,categoryId,date,content,id]);
}